package com.fengze.shop.utils;

import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

@Slf4j
public final class StringTools extends StringUtils {


    private static int timeStampLength = 4;

    public static Map<String, String> stringArrayToMap(String keys, String[] values) {
        Map<String, String> map = new LinkedHashMap<String, String>();
        String[] key = keys.split(",");
        if (key.length != values.length) {
            return null;
        }
        for (int i = 0; i < key.length; i++) {
            map.put(key[i], values[i]);
        }
        return map;
    }



    public static String replace(String srcStr, String oldStr, String newStr) {

        if (srcStr == null || srcStr.length() == 0 || oldStr == null
                || oldStr.length() == 0 || newStr == null) {
            return srcStr;
        }
        if (oldStr.equals(newStr)) {
            return srcStr;
        }

        String tmp = srcStr;
        int index = tmp.indexOf(oldStr);
        if (index != -1) {
            tmp = tmp.substring(0, index) + newStr
                    + tmp.substring(index + oldStr.length(), srcStr.length());
        }
        return tmp;
    }



    public static String getTimeStamp() {
        String starttime = String.valueOf(System.currentTimeMillis());
        String timeStamp = starttime.substring(starttime.length()
                - timeStampLength);
        return timeStamp;
    }


    public static String cutString(String inputString, int byteLen)
            throws Exception {
        String tempString = "";
        String outputString = inputString;
        if (inputString.getBytes("GBK").length > byteLen) {
            for (int i = 0; i < inputString.length(); i++) {
                tempString = tempString + inputString.substring(i, i + 1);
                if (tempString.getBytes("GBK").length > byteLen) {
                    outputString = inputString.substring(0, i);
                    break;
                }
            }
        }

        return outputString;
    }


    public static String leftFill(int totalLength, char fillChar,
                                  String srcStr, boolean chineseFlag) {
        int byteLength = (srcStr == null ? 0 : srcStr.length());
        if (chineseFlag && (byteLength > 0)) {
            try {
                byteLength = srcStr.getBytes("GBK").length;
            } catch (Exception e) {
                log.error("leftFill() string.getBytes(GBK) exception-->", e);
                return leftFill(totalLength, fillChar, null, false);
            }
        }

        int length = totalLength - byteLength;
        if (length < 0) {
            return srcStr.substring(0, totalLength);
        } else if (length == 0) {
            return srcStr;
        } else {
            char[] arr = new char[length];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = fillChar;
            }
            String sFill = new String(arr);
            if (length == totalLength) {
                return sFill;
            } else {
                return sFill + srcStr;
            }
        }
    }


    public static String rightFill(int totalLength, char fillChar,
                                   String srcStr, boolean chineseFlag) {
        int byteLength = (srcStr == null ? 0 : srcStr.length());
        if (chineseFlag && (byteLength > 0)) {
            try {
                byteLength = srcStr.getBytes("GBK").length;
            } catch (Exception e) {
                log
                        .error("rightFill() string.getBytes(GBK) exception-->",
                                e);
                return rightFill(totalLength, fillChar, null, false);
            }
        }

        int length = totalLength - byteLength;
        if (length < 0) {
            return srcStr.substring(0, totalLength);
        } else if (length == 0) {
            return srcStr;
        } else {
            char[] arr = new char[length];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = fillChar;
            }
            String sFill = new String(arr);
            if (length == totalLength) {
                return sFill;
            } else {
                return srcStr + sFill;
            }
        }
    }


    public static int parseInt(String str) {
        int number = 0;
        try {
            number = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            number = 0;
        }
        return number;
    }
}
